package com.sj.proxy;

import java.lang.reflect.Method;

public class TimeHandler implements InvocationHandler {
    private Object target; //被代理对象

    public TimeHandler(Object target) {
        this.target = target;
    }

    /**
     * @param object 这里没用到
     *               Proxy中的h.invoke(this, md)其实this没用到，this的指向是代理对象。可以print看下：com.sj.proxy.TankTimeProxy
     * @param method
     */
    public void invoke(Object object, Method method) {
        long start = System.currentTimeMillis();
        System.out.println("TimeHandler.starttime: " + start);
//        System.out.println(object.getClass().getName());    //com.sj.proxy.TankTimeProxy

        try {
            method.invoke(target); //对被代理对象调用m方法
        } catch (Exception e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("time: " + (end - start));
    }
}
